<template>
  <a-modal
    width="45vw"
    :closable="false"
    v-model:visible="visible"
    :mask-closable="false"
    :esc-to-close="false"
    unmount-on-close
    @close="onCancel"
  >
    <template #title> 修改费用</template>
    <page-form :model="form" ref="formRef" :rules="rules" layout="vertical">
      <div>
        <page-form-item label="借调单号"> {{ form.businesCode }} </page-form-item>
      </div>
      <div>
        <page-form-row :gutter="24">
          <page-form-item :label="label" size="1/2">
            <i-input-number v-model="form.totalAmount" unit="元" :precision="2" />
          </page-form-item>
          <page-form-item label="收支类型" v-if="createNode == 4" size="1/2">
            <a-select v-model="form.costType" :options="dict.costTypeEnum" :allow-clear="false" />
          </page-form-item>
        </page-form-row>
      </div>
    </page-form>
    <template #footer>
      <a-space>
        <a-button @click="onCancel">关闭</a-button>
        <a-button type="primary" :loading="loading" @click="submitFn">确认</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { ref, computed } from 'vue'
  import { getEditFeeDetailByType0, editFeeDetailForType0 } from '@/api/asset/carTransfer'
  import { Message } from '@arco-design/web-vue'
  import { usePageDict } from '@/hooks/useDict'
  const dict = usePageDict('carTransferDict')
  const form = ref({})
  const visible = ref(false)
  const rules = {}
  const loading = ref(false)
  const type = ref(0)
  const formRef = ref(null)
  const createNode = ref()
  const open = (options, flag) => {
    console.log(options, flag)
    visible.value = true
    type.value = flag
    createNode.value = options.createNode
    getDetailApi(options)
  }

  const onCancel = () => {
    visible.value = false
    formRef.value.resetFields()
    loading.value = false
  }

  const label = computed(() => {
    let label = '借调费'
    if (type.value == 0) {
      label = '借调费'
    }

    if (type.value == 1) {
      label = '借调费(借入)'
    }

    if (type.value == 2) {
      label = '借调费(借出)'
    }
    if (createNode.value == 4) {
      label = '借调附加费'
    }
    return label
  })

  const getDetailApi = (options) => {
    getEditFeeDetailByType0({ businesId: options.businesId, feeCategory: 6, id: options.id }).then((res) => {
      if (res.code == 200) {
        let result = res.result[0]
        form.value.id = result.id
        form.value.businesCode = result.businesCode
        form.value.businesId = result.businesId
        form.value.feeCategory = result.feeCategory
        form.value.totalAmount = result.totalAmount
        form.value.costType = result.costType
      }
    })
  }

  const emits = defineEmits(['refreshData'])
  const submitFn = () => {
    formRef.value.validate((error) => {
      if (!error) {
        loading.value = true
        editFeeDetailForType0({ ...form.value })
          .then((res) => {
            if (res.code == 200) {
              Message.success('操作成功')
              onCancel()
              emits('refreshData')
            }
          })
          .catch(() => {
            loading.value = false
          })
      }
    })
  }

  defineExpose({
    open,
  })
</script>
